<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="testenter">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="条形码编号">
              <a-input @change = "handleChange" v-model="WashingCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="仓库选择">
              <a-radio-group v-model="wareHouseradio" name="radioGroup">
                <a-radio value="A">A:条码打印</a-radio>
                <a-radio value="B">B:条码出库</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('条码')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
      <a-button @click="batchDuplicateChecking" type="primary" icon="plus">批量查重</a-button>
      <a-button @click="resetOkAndNgValue" type="primary">重置OK及NG值</a-button>
      <h2 style="font-size: 50px;  margin-bottom: 0px;">{{ remessage1 }}</h2>
      <h2 style="font-size: 50px; color: rgb(20, 235, 49); margin-bottom: 0px;">{{ remessage2 }}</h2>
      <h2 style="font-size: 50px; color: red; margin-bottom: 0px; ">{{ remessage3 }}</h2>
    </div>
    
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :rowClassName = "setRowClass"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">


        <template slot="isNG" slot-scope="text">
          <a-tag v-if="text == 'OK'" color="blue">{{text}}</a-tag>
          <a-tag v-else color="red">{{text}}</a-tag>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleEdit(record)">编辑</a> -->
          <!-- <a-divider type="vertical" /> -->
          
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <qrcode-modal ref="modalForm" @ok="modalFormOk"></qrcode-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import QrcodeModal from './modules/QrcodeModal'
  import { getAction,postAction } from '@/api/manage'

  export default {
    name: 'QrcodeList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      QrcodeModal
    },
    data () {
      return {
        description: '条码管理页面',
        WashingCode:'',
        wareHouseradio:'A',
        bs:'',
        //接收message消息
        remessage1:'',
        remessage2:'',
        remessage3:'',
        /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
        queryParam: {
                modelCode: '',
                itemName: ''
            },
            /* 数据源 */
          dataSource: [],
          footerDataSource:[],
          //显示文字内容标识
        //分页参数
        ipagination: {
                // current: 1,
                // pageSize: 10,
                // pageSizeOptions: ['10', '20', '30'],
                current: 1,
                pageSize: 10,
                pageSizeOptions: ['10', '20', '30', '100', '200', '300', '400', '500'],
                showTotal: (total, range) => {
                    console.log('range', range);
                    return range[0] + "-" + range[1] + " 共" + total + "条"
                },
                showQuickJumper: true,
                showSizeChanger: true,
                total: 0,
            },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'条码内容',
            align:"center",
            dataIndex: 'content'
          },
          {
            title:'仓库',
            align:"center",
            dataIndex: 'wareHouse'
          },
          {
            title:'入库扫描时间',
            align:"center",
            dataIndex: 'scanTime'
          },
          {
            title:'出库扫描时间',
            align:"center",
            dataIndex: 'scanOutTime'
          },

          {
            title:'判定结果',
            align:"center",
            dataIndex: 'result',
            scopedSlots: { customRender: 'isNG' },

          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          add: "/ch/qrcode/add",
          list: "/ch/qrcode/list",
          delete: "/ch/qrcode/delete",
          deleteBatch: "/ch/qrcode/deleteBatch",
          exportXlsUrl: "/ch/qrcode/exportXls",
          importExcelUrl: "ch/qrcode/importExcel",
          resetOkAndNgValue:"/ch/qrcode/resetOkAndNgValue",
          
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      setRowClass(record) {
        if(record.result.slice(0,7) == 'NG :该条码'){
          return "rowClass";
      }else if(record.result.slice(0,7) == 'NG:重复出库'){
        return "rowClass1";
      }
      else{
        return "";
      }
    },
      testenter(){
        postAction(this.url.add,{wareHouse:this.wareHouseradio,content:this.WashingCode}).then((res)=>{
          if(res.success){
            console.log(res.result);
            this.loadData()
          }
        })
        this.bs = 1
      },
      //重写loadData
      loadData(arg) {
            if (!this.url.list) {
                this.$message.error("请设置url.list属性!")
                return
            }
            //加载数据 若传入参数1则加载第一页的内容
            if (arg === 1) {
                this.ipagination.current = 1;
            }
            // -----------------
            var params = this.getQueryParams(); //查询条件
            params.allocator = params.allocator1
            this.loading = true;
            getAction(this.url.list, params).then((res) => {
                if (res.success) {
                    //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
                    this.dataSource = res.result.records || res.result;
                    this.footerDataSource[0] = this.dataSource[0];
                    this.planForm = this.dataSource
                    this.remessage1 = res.message.slice(0,2)
                    this.remessage2 = res.message.slice(2,res.message.indexOf(' '))
                    this.remessage3 = res.message.slice(res.message.indexOf(' '))
                    //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
                    if (res.result.total) {
                        this.ipagination.total = res.result.total;
                    } else {
                        this.ipagination.total = 0;
                    }
                    //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
                } else {
                    this.dataSource = null;
                    this.ipagination.total = 0;
                    this.$message.warning(res.message)
                }
            }).finally(() => {
                this.loading = false
            })
        },
      handleChange(){
        if(this.bs == 1){
          this.WashingCode = this.WashingCode.charAt(this.WashingCode.length - 1)
          this.bs = 0
        }
      },
      batchDuplicateChecking(){
        postAction('/ch/qrcode/batchDuplicateCheck').then((res) => {
          if (res.success) {
            this.loadData();
            this.$message.success(res.message);
          } 
                })
      },
      //重置OK和NG值
      resetOkAndNgValue(){
        getAction(this.url.resetOkAndNgValue,{wareHouse:this.wareHouseradio}).then((res)=>{
          if(res.success){
            
            this.loadData()
          }
        })
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'rowid',text:'rowid',dictCode:''})
        fieldList.push({type:'string',value:'content',text:'条码内容',dictCode:''})
        fieldList.push({type:'string',value:'wareHouse',text:'仓库',dictCode:''})
        fieldList.push({type:'datetime',value:'scanTime',text:'入库扫描时间'})
        fieldList.push({type:'datetime',value:'scanOutTime',text:'出库扫描时间'})
        fieldList.push({type:'string',value:'result',text:'判定结果',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style>
  @import '~@assets/less/common.less';

  .rowClass{
    background-color: #d3475a;
  }
  .rowClass1{
    background-color: #c47a84;
  }
</style>